<?php
/** 
 * Created by RisingSun. 
 * User: MarkSpace 
 * Date: 20210822
 * Time: 11:05 
 * 矿机管理
 */
namespace app\settle\model;

use app\settle\model\Model;
use Mscore\Core\Redis;

class Tradeorder extends Model{
    
    private $orderlog = 'pro_tradeorderlog';
    private $account = 'user_account';
    private $order = 'pro_tradeorder';
    private $hashfix = '';
    private $key = '';

    public function __construct()  
    {    
        $this->hashfix=config('redis.redis_db.db_hashfix');
        $this->key = config('tradecontrol.order');
    }  

    /**
     * 列表
     * @param array $list 
     * @param array $where 
     */ 
    public function list($where=null,$page=1,$page_size=10){
        //矿机列表
        $list['datalist']= Model::name($this->order)
            ->alias('a')
            ->join($this->account.' b','a.aid=b.aid','left')
            ->field('a.*, b.username, b.realname')
            ->where($where)
            ->limit(($page-1)*$page_size,$page_size)
            ->order('a.createtime desc ')
            ->select()
            ->toArray();
        foreach ($list['datalist'] as $key => $value) {
            $list['datalist'][$key]['reset']=2;
            $list['datalist'][$key]['rstatus']=2; 
            switch ($value['status']) {
                case 1:
                    $datasub= Model::name($this->orderlog)->field('id,status')->where('status=1 and orderid='.$value['id'])->select()->toArray();
                    if(empty($datasub)){
                        continue 2;
                    }
                    foreach ($datasub as $k => $val) {
                        $data = Redis::exec('hget',[$this->hashfix.$this->key,$val['id']]);
                        if(!empty($data)){
                            $data=json_decode($data,true);
                            if($data['warn']==1){
                               $list['datalist'][$key]['rstatus']=1;
                               break; 
                            }
                        }else{
                            $list['datalist'][$key]['reset']=1;
                        }
                    }
                    break;
                
                case 2:
                    $list['datalist'][$key]['reset']=2;
                    $list['datalist'][$key]['rstatus']=2; 
                    break;
            }
        }
        $cmf_arr = array_column($list['datalist'], 'reset');//抽取排序字段
        // array_multisort($cmf_arr, SORT_DESC, $list['datalist']); //根据字段排序SORT_ASC - 默认。按升序排列 (A-Z)；SORT_DESC - 按降序排列 (Z-A)。
        //数据总数
        $list['total']=Model::name($this->order)
            ->alias('a')
            ->join($this->account.' b','a.aid=b.aid','left')
            ->where($where)
            ->count('id');
        return $list;
    }

    /**
     * 信息
     * @param array $info 
     * @param array $where 
     */ 
    public function info($id=null){  
        $info = Model::name($this->order)
           ->alias('a')
           ->join($this->account.' b','a.aid=b.aid','left')
           ->field('a.*, b.realname,b.mobile,b.username,b.email')
           ->where(['id'=>$id])
           ->find();
        return $info;
    }

    /**
     * 信息
     * @param array $info 
     * @param array $where 
     */ 
    public function orderinfo($where=null,$field='*'){  
        $info = Model::name($this->order)
           ->field($field)
           ->where($where)
           ->find();
        return $info;
    }

    /**
     * 信息
     * @param array $info 
     * @param array $where 
     */ 
    public function userinfo($where=null){  
         return Model::name($this->account)
           ->where($where)
           ->value('aid');
    }

    /**
     * 总计信息
     * @param array $suminfo 
     * @param array $where 
     */ 
    public function suminfo($where=null,$field='a.*'){  
         return Model::name($this->orderlog)
           ->alias('a')
           ->field($field)
           ->where($where)
           ->find();
    }
    
    /**
     * 信息
     * @param array $info 
     * @param array $where 
     */ 
    public function infolog($where=null,$field='*'){  
        $data = Model::name($this->orderlog)
           ->field($field)
           ->where($where)
           ->find();
        return $data;
    }

    /**
     * 信息
     * @param array $info 
     * @param array $where 
     */ 
    public function infologlist($where=null,$field='*'){  
        $data = Model::name($this->orderlog)
            ->field($field)
            ->where($where)
            ->select()
            ->toArray();
            foreach ($data as $key => $value) {
                $data[$key]['reset']=2;
                if($value['status']==1){
                    $datasub = Redis::exec('hget',[$this->hashfix.$this->key,$value['id']]);
                    if(!empty($datasub)){
                        $datasub = json_decode($datasub,true);
                        $data[$key]['wlpoint'] = isset($datasub['difference'])?$datasub['difference']:0;
                        $data[$key]['money'] = $datasub['money'];
                        $data[$key]['warn'] = $datasub['warn'];
                        $data[$key]['sellprice'] = isset($datasub['sellprice'])?$datasub['sellprice']:0;
                    }else{
                        $data[$key]['reset']=1;
                    }
                }
                $data[$key]['point']=!empty($value['point'])?json_decode($value['point'],true):[];
            }
        return $data;
    }
    /**
     * 保存
     * @param array $save 
     * @param array $where 
     */ 
    public function setdisk($data=[]){
        if(empty($data['id'])){
            return fasle;
        }
        $info = Model::name($this->orderlog)->field('*')->where('id='.$data['id'])->find();
        $user=Model::name($this->account)->where('aid='.$info['aid'])->field('amount,giftamount')->find();
        if(!empty($info)&&$info['puffs']>0){
            $order= Model::name($this->order)->field('rands,price,type,money')->where('id='.$info['orderid'])->find();
            $difference = round($data['money']/$info['puffs']/$order['price'],2);
            $sellprice = $info['buyprice']+$difference*$order['rands'];
            switch ($order['type']) {
                case 1:
                    Model::name($this->account)->where('aid='.$data['aid'])->update(['amount'=>($user['amount']-$info['money']+$data['money'])]);
                break;
                default:
                Model::name($this->account)->where('aid='.$data['aid'])->update(['giftamount'=>($user['giftamount']-$info['money']+$data['money'])]);
                break;
            }
            switch (true) {
                case $data['money']>0:
                    $win=1;
                    break;
                case $data['money']<0:
                    $win=2;
                    break;
                default:
                    $win=0;
                    break;
            }
            Model::name($this->orderlog)->where('id='.$data['id'])->update(['sellprice'=>$sellprice,'wlpoint'=>$difference,'money'=>$data['money'],'win'=>$win]);
            Model::name($this->order)->where('id='.$data['id'])->update(['money'=>($order['money']-$info['money']+$data['money'])]);
        }
        return true;
        
    }

    /**
     * 保存
     * @param array $save 
     * @param array $where 
     */ 
    public function control($data=[]){
        if(empty($data['id'])){
            return fasle;
        }
        $info = Model::name($this->orderlog)->field('*')->where('id='.$data['id'])->find();
        if(!empty($info)&&$info['puffs']>0){
            Model::name($this->orderlog)->where('id='.$data['id'])->update(['drift'=>$data['drift']]);
            $info = Redis::exec('hget',[$this->hashfix.$this->key,$info['id']]);
            if(!empty($info)){
                $info = json_decode($info,true);
                $info['drift'] = $data['drift'];
                Redis::exec('hset',[$this->hashfix.$this->key,$info['id'],json_encode($info)]);
            }
            unset($data,$info);
        }
        return true;
        
    }

    /**
     * 删除
     * @param array $del 
     * @param array $where 
     */
    public function del($id=null){
     //删除
        if ($id) {
            Model::name($this->order)->where('id='.$id)->update(['dels'=>1]);
            return true;
        }
        return false;
    }

    /**
     * 操作redis
     * @param array $execRedis
     * @param array $where $page $num
     */
    public function execRedis($meth,$arr,$db='0')
    {
        if($meth){
            $arr[0]=$this->hashfix.$arr[0];
            $data = Redis::exec($meth,$arr,$db);
            return $data;
        }        
        return false;
    }

}
